import pygame
import math
import random
from pygame.locals import *
from Player import Player
from Arrow import Arrow
from BadGuy import BadGuy
from TextUtil import TextUtil
from AudioManager import AudioManager

class Game(object):
    def __init__(self):
        pygame.init()
        pygame.key.set_repeat(2000, 0)
        
        self.width,self.height = 640,480
        self.window= pygame.display.set_mode((self.width, self.height))
        self.keys = [False,False,False,False]
        
        self.player = Player()
        self.textUtil = TextUtil()
        self.audios = AudioManager()
        self.arrs = pygame.sprite.RenderUpdates()
        self.badGuys = pygame.sprite.RenderUpdates()

        self.badtimer = 100
        self.badtimer1 = 0
        self.healthvalue = 195

        self.loadResource()

        self.running = 1
        self.exitcode = 0

    def loadResource(self):
        self.grass = pygame.image.load('resources/images/grass.png').convert()
        self.castle = pygame.image.load("resources/images/castle.png")
        self.arrow = pygame.image.load("resources/images/bullet.png")
        self.badguyimg = pygame.image.load("resources/images/badguy.png")
        self.healthvaluebar = pygame.image.load("resources/images/healthbar.png")
        self.health = pygame.image.load("resources/images/health.png")
        self.gameover = pygame.image.load("resources/images/gameover.png")
        self.youwin = pygame.image.load("resources/images/youwin.png")

    def key_down_event(self,key):
        keys = self.keys
        if  key== K_w:
            keys[0] = True
        elif key == K_a:
            keys[1] = True
        elif key == K_s:
            keys[2] = True
        elif key == K_d:
            keys[3] = True

    def key_up_event(self,key):
        keys = self.keys
        if key == K_w:
            keys[0] = False
        elif key ==K_a:
            keys[1] = False
        elif key == K_s:
            keys[2] = False
        elif key == K_d:
            keys[3] = False

    def player_move(self):
        playerpos = self.player.playerPos
        keys = self.keys
        if keys[0]:
            playerpos[1]-=2
        elif keys[1]:
            playerpos[0]-=2
        elif keys[2]:
            playerpos[1]+=2
        elif keys[3]:
            playerpos[0]+=2

        if playerpos[0] < 0:
            playerpos[0] = 0
        elif playerpos[0] > self.width - self.player.image.get_width():
            playerpos[0] = self.width - self.player.image.get_width()
        
        if playerpos[1] < 0:
            playerpos[1] = 0
        elif playerpos[1] > self.height - self.player.image.get_height():
            playerpos[1] = self.height - self.player.image.get_height()

    def mouse_down(self):
        arrow = Arrow(self.arrow,self.player.newPos)
        self.audios.shoot.play()
        self.arrs.add(arrow)

    def display(self):
        window = self.window
        
        #background
        for x in range(self.width/self.grass.get_width()+1):
            for y in range(self.height/self.grass.get_height()+1):
                window.blit(self.grass,(x*100,y*100))
        window.blit(self.castle,(0,30))
        window.blit(self.castle,(0,135))
        window.blit(self.castle,(0,240))
        window.blit(self.castle,(0,345 ))

        #player
        self.player.update()

        #arrow
        for arrs in self.arrs:
            arrs.update((self.width,self.height))

        #badGuy
        for bad in self.badGuys:
            remainHealth = bad.update((self.width,self.height,self.arrs,self.healthvalue))
            if remainHealth != None:
                self.healthvalue = int(remainHealth)
                self.audios.hit.play()
        
        #health
        window.blit(self.healthvaluebar, (5,5))
        for health1 in range(self.healthvalue):
            window.blit(self.health ,(health1+8,8))   

        #Clock
        self.textUtil.Clock()

    def StatusHandle(self):
        if pygame.time.get_ticks() >= 90000:
            self.running = 0
            self.exitcode = 1
        if self.healthvalue <= 0:
            self.running = 0
            self.exitcode = 0      

        if self.running == 0:
            if self.exitcode == 0:
                self.window.blit(self.gameover, (0,0))
            else:
                self.window.blit(self.youwin, (0,0))


    def run(self):
        while self.running:
            for event in pygame.event.get():
                
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                elif event.type == pygame.KEYDOWN :
                    self.key_down_event(event.key)
                elif event.type == pygame.KEYUP:
                    self.key_up_event(event.key)
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    self.mouse_down()

            self.badtimer -= 1
            if self.badtimer == 0:
                badGuy = BadGuy(self.badguyimg,\
                                self.width,\
                                random.randint(50,self.height-50))
                self.audios.enemy.play()
                self.badGuys.add(badGuy)
                self.badtimer = 100 - (self.badtimer1*2)
                if self.badtimer1 >= 35:
                    self.badtimer1 = 35
                else:
                    self.badtimer1 += 5

            self.player_move()
            self.display()
            self.StatusHandle()
            pygame.display.flip()
    
